package com.sailfish.springbootdemo.controller.db2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbParentFactoryInfo;
import com.sailfish.springbootdemo.service.db2.PcbParentFactoryInfoService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/pcbParentFactoryInfo")
public class PcbParentFactoryInfoController {

    @Autowired
    private PcbParentFactoryInfoService pcbParentFactoryInfoService;

    @PostMapping("/add")
    public Result insert(String companyName, String companyNameFull, String companyNameEn, String companyCode, String companyWebsite, Integer status, Integer sort, String remarkPic) {
        //判断一些必填项
        if (StringUtils.isBlank(companyName) && StringUtils.isBlank(companyNameEn)) {
            return ResultUtil.error(500, "缺少参数companyName", "Miss Param companyName", null);
        }
        try {
            status = status == null ? 1 : status;
            PcbParentFactoryInfo pcbParentFactoryInfo = new PcbParentFactoryInfo();
            pcbParentFactoryInfo.setCompanyName(companyName);
            pcbParentFactoryInfo.setCompanyNameFull(companyNameFull);
            pcbParentFactoryInfo.setCompanyNameEn(companyNameEn);
            pcbParentFactoryInfo.setCompanyWebsite(companyWebsite);
            pcbParentFactoryInfo.setCompanyCode(companyCode);
            java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
            pcbParentFactoryInfo.setCreateTime(date);
            pcbParentFactoryInfo.setStatus(status);
            pcbParentFactoryInfo.setSort(sort);
            pcbParentFactoryInfo.setRemarkPic(remarkPic);
            return pcbParentFactoryInfoService.insert(pcbParentFactoryInfo);
        } catch (Exception e) {
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @PostMapping("/getAllCompany")
    public Result getAllCompany() {
        List<PcbParentFactoryInfo> pcbParentFactoryInfoList = pcbParentFactoryInfoService.getAll();
//        List<String> pcbParentFactoryNameList = pcbParentFactoryInfoList.stream().map(x -> x.getCompanyNameFull()).collect(Collectors.toList());
//        Map<String,String> map = new HashMap<>();
//        for(int i=0;i<pcbParentFactoryNameList.size();i++){
//            map.put("companyName",pcbParentFactoryNameList.get(i));
//        }
        return ResultUtil.success(pcbParentFactoryInfoList);
    }

    @PostMapping("/getAll")
    public Result getAll(String searchK, String searchV, Integer status) {
        String companyName = null;
        String companyNameEn = null;
        String companyWebsite = null;
        String companyNameFull = null;
        String companyCode = null;
        if (!StringUtils.isBlank(searchK)) {
            if (searchK.equals("companyCode")) {
                companyCode = searchV;
            }
            if (searchK.equals("companyNameFull")) {
                companyNameFull = searchV;
            }
            if (searchK.equals("companyName")) {
                companyName = searchV;
            }
            if (searchK.equals("companyNameEn")) {
                companyNameEn = searchV;
            }
            if (searchK.equals("companyWebsite")) {
                companyWebsite = searchV;
            }
        }
        List<PcbParentFactoryInfo> children = pcbParentFactoryInfoService.getAll(companyName, companyNameFull, companyCode, companyNameEn, companyWebsite, null, null, null, null, null, null, null, status, null);
        return ResultUtil.success(children);
    }

    @PostMapping("/getBySonList")
    public Result getBySonList(@RequestBody String data) {
        try {
            List<Integer> sonIds = JSONArray.parseArray(data, Integer.class);

            List<PcbParentFactoryInfo> children = pcbParentFactoryInfoService.getBySonList(sonIds, null);
            return ResultUtil.success(children);
        } catch (Exception e) {
            List<PcbParentFactoryInfo> children = pcbParentFactoryInfoService.getBySonList(null, null);
            return ResultUtil.success(children);
        }
    }

    @PostMapping("/getBySonList2")
    public Result getBySonList2(@RequestBody String data) {
        try {
            List<Integer> sonIds = JSONArray.parseArray(data, Integer.class);

            List<PcbParentFactoryInfo> children = pcbParentFactoryInfoService.getBySonList(sonIds, 1);
            return ResultUtil.success(children);
        } catch (Exception e) {
            List<PcbParentFactoryInfo> children = pcbParentFactoryInfoService.getBySonList(null, 1);
            return ResultUtil.success(children);
        }
    }

    @PostMapping("/delete")
    public Result deleteById(Integer id) {
        if (id == null) {
            return ResultUtil.error(500, "参数id为空", "param id is null", null);
        }
        return pcbParentFactoryInfoService.delete(id);
    }

    @PostMapping("/update")
    public Result update(@RequestBody String data) {

        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("pcbParentFactoryId");
        String companyName = jsonObject.getString("companyName");
        String companyNameEn = jsonObject.getString("companyNameEn");
        String companyNameFull = jsonObject.getString("companyNameFull");
        String companyWebsite = jsonObject.getString("companyWebsite");
        String companyCode = jsonObject.getString("companyCode");
        String remarkPic = jsonObject.getString("remarkPic");
        Date createTime = jsonObject.getDate("createTime");
        Integer status = jsonObject.getInteger("status");
        Integer sort = jsonObject.getInteger("sort");
        if (id == null) {
            return ResultUtil.error(500, "参数id为空", "id is null", null);
        }
        PcbParentFactoryInfo item = new PcbParentFactoryInfo();
        item.setPcbParentFactoryId(id);
        item.setCompanyName(companyName);
        item.setCompanyNameFull(companyNameFull);
        item.setCompanyNameEn(companyNameEn);
        item.setCompanyWebsite(companyWebsite);
        item.setCompanyCode(companyCode);
        item.setCreateTime(createTime);
        item.setStatus(status);
        item.setSort(sort);
        item.setRemarkPic(remarkPic);
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        item.setUpdateTime(date);
        return pcbParentFactoryInfoService.update(item);
    }

    @PostMapping("/checkHasSonFactory")
    public Result checkHasSonFactory(Integer factoryId) {
        if (factoryId == null) {
            return ResultUtil.error(500, "参数factoryId为空", "param factoryId is null", null);
        }
        boolean re = pcbParentFactoryInfoService.checkHasSonFactory(factoryId);
        return ResultUtil.success(re);
    }
}
